package com.adaptiveAgingAdmin.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 这是一个基于RedisTemplate进行封装redis的静态工具类，主要用于简化redis的常用操作
 *
 * @author 张涛
 */
@Component
public class RedisUtil {

    public static RedisTemplate redisTemplate;
    public static final String ACCESS_TOKEN_PREFIX="access-token:";
    public static final String REFRESH_TOKEN_PREFIX="refresh-token:";
    public static final String ID_PREFIX="admin-id:";

    @Autowired
    private void setRedisTemplate(RedisTemplate redisTemplate){
        RedisUtil.redisTemplate=redisTemplate;
    }

    /**
     *
     * @param key
     * @return
     */
    public static Boolean hasKey(String key){
        try {
            return redisTemplate.hasKey(key);
        }catch (Exception e){
            return Boolean.FALSE;
        }
    }

    /**
     *
     * @param key String
     * @param value Object
     * @return 返回Boolean
     */
    public static Boolean set(String key,Object value){
        try {
            redisTemplate.opsForValue().set(key, value);
            return Boolean.TRUE;
        }catch (Exception e){
            e.printStackTrace();
            return Boolean.FALSE;
        }
    }

    /**
     *
     * @param key
     * @param value
     * @param time
     * @return
     */
    public static Boolean set(String key,Object value,Long time){
        try {
            if(time>0){
                redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
            }
            else {
                redisTemplate.opsForValue().set(key, value);
            }
            return Boolean.TRUE;
        }catch (Exception e){
            e.printStackTrace();
            return Boolean.FALSE;
        }
    }

    /**
     *
     * @param key String
     * @return <T>根据接收类型，自动强转
     */
    public static <T> T get(String key){
        return null==key?null: (T) redisTemplate.opsForValue().get(key);
    }
    public static Boolean delete(String... keys){
        try {
            if(null!=keys && keys.length>0){
                if (keys.length==1){
                    redisTemplate.delete(keys[0]);
                }
                else {
                    redisTemplate.delete(CollectionUtils.arrayToList(keys));
                }
            }
            return Boolean.TRUE;
        }catch (Exception e){
            e.printStackTrace();
            return Boolean.FALSE;
        }
    }

    /**
     *
     * @param key
     * @param time
     * @return
     */
    public static Boolean expire(String key,Long time){
        try {
            if(time>0){
                redisTemplate.expire(key,time,TimeUnit.SECONDS);
            }
            else {
                redisTemplate.expire(key,-1,TimeUnit.SECONDS);
            }
            return Boolean.TRUE;
        }catch (Exception e){
            return Boolean.FALSE;
        }
    }

    /**
     *
     * @param key
     * @return
     */
    public static Long getExpire(String key){
        return redisTemplate.getExpire(key);
    }

    /**
     *
     * @param key
     * @param map
     * @return
     */
    public static Boolean setHashMap(String key, Map<String,Object> map){
        try {
            redisTemplate.opsForHash().putAll(key, map);
            return Boolean.TRUE;
        }catch (Exception e){
            e.printStackTrace();
            return Boolean.FALSE;
        }
    }

    /**
     *
     * @param key
     * @param map
     * @param time
     * @return
     */
    public static Boolean setHashMap(String key, Map<String,Object> map,Long time){
        try {
            redisTemplate.opsForHash().putAll(key, map);
            Boolean flag=Boolean.TRUE;
            if(time>0) {
                flag=expire(key,time);
            }
            return flag;
        }catch (Exception e){
            e.printStackTrace();
            return Boolean.FALSE;
        }
    }

    /**
     *
     * @param key
     * @return
     */
    public static Map<Object,Object> getHashMap(String key){
        return redisTemplate.opsForHash().entries(key);
    }

    /**
     *
     * @param key
     * @param hashKey
     * @return
     */
    public static <T>T getHashValue(String key,String hashKey){
        return (T)redisTemplate.opsForHash().get(key,hashKey);
    }
}
